package de.velvettear.squirrel.util;

import java.util.Vector;

import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.util.Log;
import de.velvettear.squirrel.kernel.Statics;

public class BruteForceHelper {

	private static Vector<Integer> bruteForceChars;
		
	public static long getPossibilityCount(int max) {
		Log.i("getPossibilityCount()", "BEGIN");
		long possibilities = Double.valueOf(Math.pow(bruteForceChars.size(), max)).longValue();
		Log.i("getPossibilityCount()", "POSSIBILITIES " + possibilities);
		Log.i("getPossibilityCount()", "END");
		return possibilities;
	}
	
	public static int getProgressPercentage(long current, long max) {
		Log.i("getPossibilityCount()", "BEGIN");
		int percentage = (int) (((double) current / (double) max) * 100);
		Log.i("getPossibilityCount()", "PERCENTAGE" + percentage);
		Log.i("getPossibilityCount()", "END");
		return percentage;
	}
	
	public static Vector<Integer> getBruteForceChars(boolean lowerCase, boolean upperCase, boolean numeric, boolean specialChars) {
		Log.i("getBruteForceChars()", "BEGIN");
		bruteForceChars = new Vector<Integer>();
		if (lowerCase) {
			for (int counter = Statics.CHARACTERS_LOWERCASE_FIRST; counter <= Statics.CHARACTERS_LOWERCASE_LAST; counter++) {
				bruteForceChars.add(counter);
			}
		}
		if (upperCase) {
			for (int counter = Statics.CHARACTERS_UPPERCASE_FIRST; counter <= Statics.CHARACTERS_UPPERCASE_LAST; counter++) {
				bruteForceChars.add(counter);
			}
		}
		if (numeric) {
			for (int counter = Statics.CHARACTERS_NUMERIC_FIRST; counter <= Statics.CHARACTERS_NUMERIC_LAST; counter++) {
				bruteForceChars.add(counter);
			}
		}
		if (specialChars) {
			for (int counter = 0; counter < Statics.CHARACTERS_SPECIALCHARS.length; counter++) {
				bruteForceChars.add(Statics.CHARACTERS_SPECIALCHARS[counter]);
			}
		}
		
		Log.i("getBruteForceChars()", "END");
		return bruteForceChars;
	}
	
	public static char first() {
		int first = bruteForceChars.firstElement();
		return (char) first;
	}

	public static char last() {
		int last = bruteForceChars.lastElement();
		return (char) last;
	}

	private static char nextChar(char c) {
        if (c == last()) {
            return first();
        } else {
			int index = bruteForceChars.indexOf((int) c) + 1;
			int charNumber = bruteForceChars.elementAt(index);
			return (char) charNumber;
        }
    }
	
	public static String next(String current) {
		String next = "";
		int length = current.length();
		char[] chars = current.toCharArray();
		for (int counter = length - 1; counter >= 0; counter--) {
			chars[counter] = nextChar(chars[counter]);
			if (!(chars[counter] == last())) {
				next = new String(chars);
				break;
			} else {
				if (counter == 0) {
					next = new String(chars) + first();
				}
			}
		}
		
		return next;
	}
	
}
